<?php

use Illuminate\Database\Capsule\Manager as DB;

class NewsController extends CommonController
{
    protected $authExcept = ['index', 'detail'];

    public function indexAction()
    {
        $page = $this->get('page', 1);
        $per_page = $this->get('per_page', 10);
        $offset = ($page - 1) * $per_page;

        $total = newsModel::query()->count();
        $data = newsModel::query()
            ->where('status', 1)
            ->orderBy('recommend', 'desc')
            ->orderBy('sortorder', 'desc')
            ->orderBy('id', 'desc')
            ->offset($offset)
            ->limit($per_page)
            ->get();

        $result = [
            'current_page' => $page,
            'last_page' => ceil($total / $per_page),
            'per_page' => $per_page,
            'total' => $total,
            'data' => $data,
        ];

        json(['code' => 1, 'msg' => '新闻列表', 'data' => $result]);
    }

    public function detailAction()
    {
        $id = $this->get('id', 0);
        $data = newsModel::query()->with(['news_content'])->find($id);
        json(['code' => 1, 'msg' => '新闻详情', 'data' => $data]);
    }
}
